/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//layout_cofactor.hpp

#ifndef MVL_LAYOUT_COFACTOR_HPP
#define MVL_LAYOUT_COFACTOR_HPP

#include "layout_project.hpp"

namespace mvl {

namespace layout {

//create a layout that is the same as the original but with row I and column J removed
template<typename L, typename I, typename J>
struct layout_cofactor {

    typedef L layout_t;
    typedef typename layout_t::order order;
    typedef typename get1<I, J, order>::type rm_pos1;
    typedef typename get2<I, J, order>::type rm_pos2;

    struct fn1 {
        template<typename Pos1>
        struct apply : meta::not_equal_to<Pos1, rm_pos1> { };
    };

    struct fn2 {
        template<typename Pos1, typename Pos2>
        struct apply : meta::not_equal_to<Pos2, rm_pos2> { };
    };

    struct new_fn {
        template<typename Entry>
        struct apply { 
            typedef typename meta::if_<
                typename meta::less<typename Entry::i, I>::type,
                typename Entry::i,
                typename meta::minus<typename Entry::i, meta::int_<1> >::type
            >::type new_i;

            typedef typename meta::if_<
                typename meta::less<typename Entry::j, J>::type,
                typename Entry::j,
                typename meta::minus<typename Entry::j, meta::int_<1> >::type
            >::type new_j;
            typedef project_entry<new_i, new_j, typename Entry::tag, Entry> type;
        };
    };

    typedef typename get2<
        typename layout_t::Rows,
        typename layout_t::Cols,
        order
    >::type size2;
    typedef typename layout_project<
        layout_t,
        fn1,
        fn2,
        new_fn,
        typename meta::minus<size2, meta::int_<1> >::type
    >::type type;
};

} //namespace layout

} //namespace mvl


#endif

